
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/incubator-trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	"log"
	"time"
)

type StatsSummary struct {
	CdnName         string            `db:"cdn_name" json:"cdnName"`
	Deliveryservice string            `db:"deliveryservice" json:"deliveryservice"`
	StatName        string            `db:"stat_name" json:"statName"`
	StatValue       int64             `db:"stat_value" json:"statValue"`
	StatDate        time.Time         `db:"stat_date" json:"statDate"`
	CreatedAt       time.Time         `db:"created_at" json:"createdAt"`
	Links           StatsSummaryLinks `json:"_links" db:-`
}

type StatsSummaryLinks struct {
	Self string `db:"self" json:"_self"`
}

// @Title getStatsSummaryById
// @Description retrieves the stats_summary information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    StatsSummary
// @Resource /api/2.0
// @Router /api/2.0/stats_summary/{id} [get]
func getStatsSummary(cdnName string, deliveryservice string, statName string, statDate time.Time, db *sqlx.DB) (interface{}, error) {
	ret := []StatsSummary{}
	arg := StatsSummary{}
	arg.CdnName = cdnName
	arg.Deliveryservice = deliveryservice
	arg.StatName = statName
	arg.StatDate = statDate
	queryStr := "select *, concat('" + API_PATH + "stats_summary', '/cdn_name/', cdn_name, '/deliveryservice/', deliveryservice, '/stat_name/', stat_name, '/stat_date/', stat_date) as self"
	queryStr += " from stats_summary WHERE cdn_name=:cdn_name AND deliveryservice=:deliveryservice AND stat_name=:stat_name AND stat_date=:stat_date"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getStatsSummarys
// @Description retrieves the stats_summary
// @Accept  application/json
// @Success 200 {array}    StatsSummary
// @Resource /api/2.0
// @Router /api/2.0/stats_summary [get]
func getStatsSummaries(db *sqlx.DB) (interface{}, error) {
	ret := []StatsSummary{}
	queryStr := "select *, concat('" + API_PATH + "stats_summary', '/cdn_name/', cdn_name, '/deliveryservice/', deliveryservice, '/stat_name/', stat_name, '/stat_date/', stat_date) as self"
	queryStr += " from stats_summary"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postStatsSummary
// @Description enter a new stats_summary
// @Accept  application/json
// @Param                 Body body     StatsSummary   true "StatsSummary object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/stats_summary [post]
func postStatsSummary(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v StatsSummary
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO stats_summary("
	sqlString += "cdn_name"
	sqlString += ",deliveryservice"
	sqlString += ",stat_name"
	sqlString += ",stat_value"
	sqlString += ",stat_date"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":cdn_name"
	sqlString += ",:deliveryservice"
	sqlString += ",:stat_name"
	sqlString += ",:stat_value"
	sqlString += ",:stat_date"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putStatsSummary
// @Description modify an existing stats_summaryentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     StatsSummary   true "StatsSummary object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/stats_summary/{id}  [put]
func putStatsSummary(cdnName string, deliveryservice string, statName string, statDate time.Time, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg StatsSummary
	err := json.Unmarshal(payload, &arg)
	arg.CdnName = cdnName
	arg.Deliveryservice = deliveryservice
	arg.StatName = statName
	arg.StatDate = statDate
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE stats_summary SET "
	sqlString += "cdn_name = :cdn_name"
	sqlString += ",deliveryservice = :deliveryservice"
	sqlString += ",stat_name = :stat_name"
	sqlString += ",stat_value = :stat_value"
	sqlString += ",stat_date = :stat_date"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE cdn_name=:cdn_name AND deliveryservice=:deliveryservice AND stat_name=:stat_name AND stat_date=:stat_date"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delStatsSummaryById
// @Description deletes stats_summary information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    StatsSummary
// @Resource /api/2.0
// @Router /api/2.0/stats_summary/{id} [delete]
func delStatsSummary(cdnName string, deliveryservice string, statName string, statDate time.Time, db *sqlx.DB) (interface{}, error) {
	arg := StatsSummary{}
	arg.CdnName = cdnName
	arg.Deliveryservice = deliveryservice
	arg.StatName = statName
	arg.StatDate = statDate
	result, err := db.NamedExec("DELETE FROM stats_summary WHERE cdn_name=:cdn_name AND deliveryservice=:deliveryservice AND stat_name=:stat_name AND stat_date=:stat_date", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
